package com.jinke.api.modules.third.easypost.model.response;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.jinke.api.common.api.CommonResult;
import lombok.Data;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

@Data
public class CreateOrderResponse {
    private String id;
    @JsonProperty("tracking_code")
    private String trackingCode;
    @JsonProperty("selected_rate")
    private GetPriceResponseDetail selectedRate;
    @JsonProperty("postage_label")
    private PostageLabel postageLabel;
    private List<Fee> fees;

    private List<CreateOrderResponseDetail> messages;
    private boolean success = true;

    @Data
    public class Fee {

        private String object;
        private String type;
        private BigDecimal amount;
        private Boolean charged;
        private Boolean refunded;
    }
    @Data
    public static class GetPriceResponseDetail {
        private String object;
        private String mode;
        private String service;
        private String carrier;
        private String rate;
        private String currency;
        @JsonProperty("retail_rate")
        private String retailRate;
        @JsonProperty("retail_currency")
        private String retailCurrency;
        @JsonProperty("list_rate")
        private String listRate;
        @JsonProperty("list_currency")
        private String listCurrency;
        @JsonProperty("billing_type")
        private String billingType;
        @JsonProperty("delivery_days")
        private int deliveryDays;
        @JsonProperty("delivery_date")
        private String deliveryDate;
        @JsonProperty("delivery_date_guaranteed")
        private boolean deliveryDateGuaranteed;
        @JsonProperty("est_delivery_days")
        private int estDeliveryDays;
        @JsonProperty("carrier_account_id")
        private String carrierAccountId;
    }

    @Data
    public static class PostageLabel {
        @JsonProperty("object")
        private String object;

        @JsonProperty("id")
        private String id;

        @JsonProperty("created_at")
        private String createdAt;

        @JsonProperty("updated_at")
        private String updatedAt;

        @JsonProperty("date_advance")
        private int dateAdvance;

        @JsonProperty("integrated_form")
        private String integratedForm;

        @JsonProperty("label_date")
        private String labelDate;

        @JsonProperty("label_resolution")
        private int labelResolution;

        @JsonProperty("label_size")
        private String labelSize;

        @JsonProperty("label_type")
        private String labelType;

        @JsonProperty("label_file_type")
        private String labelFileType;

        @JsonProperty("label_url")
        private String labelUrl;

        @JsonProperty("label_pdf_url")
        private String labelPdfUrl;

        @JsonProperty("label_zpl_url")
        private String labelZplUrl;

        @JsonProperty("label_epl2_url")
        private String labelEpl2Url;

        @JsonProperty("label_file")
        private String labelFile;

        // Getters and setters
    }

    @Data
    public static class CreateOrderResponseDetail {
       private String message;
    }

    public CommonResult toCommonResult() {
        // 判断是否成功
        CommonResult commonResult = CommonResult.success();
        if (StringUtils.isEmpty(id)) {
            String msg = "";
            if(CollectionUtil.isNotEmpty(messages)){
                msg = messages.get(0).getMessage();
            }
            commonResult = CommonResult.failed(msg);
            success = false;
        }

        return commonResult;
    }

    public   BigDecimal findInsuranceFeeAmount() {
        Optional<Fee> insuranceFee = fees.stream()
                .filter(fee -> "InsuranceFee".equals(fee.getType()))
                .findFirst();

        return insuranceFee.map(Fee::getAmount).orElse(BigDecimal.ZERO);
    }
}
